package com.salt.modules.sys.service;

import com.google.common.collect.Lists;
import com.salt.common.service.CrudService;
import com.salt.modules.sys.entity.AppChildMenu;
import com.salt.modules.sys.entity.Menu;
import com.salt.modules.sys.entity.RightNavBar;
import com.salt.modules.sys.mapper.AppChildMenuMapper;
import com.salt.modules.sys.mapper.RightNavBarMapper;
import com.salt.modules.sys.utils.MenuUtils;
import com.salt.modules.sys.utils.UserUtils;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description mac版ui右侧导航栏管理Service
 * @Author salt
 * @Date 2022/1/7
 * @Version 1.0.1
 */
@Service
@AllArgsConstructor
public class RightNavBarService extends CrudService<RightNavBarMapper, RightNavBar> {

    private final AppChildMenuMapper appChildMenuMapper;

    public List<Menu> rightMenus(RightNavBar rightNavBar, Menu menu) {
        List<RightNavBar> list = mapper.findList(rightNavBar);
        if (list != null && !list.isEmpty()) {
            List<Menu> allMenuList = MenuUtils.getAllMenuList(menu);
            List<Menu> menuList = new ArrayList<>(list.size());
            for (RightNavBar navBar:list) {
                for (Menu m:allMenuList) {
                    if (m.getId().equals(navBar.getMenuId())){
                        menuList.add(m);
                        break;
                    }
                }
            }
            if (!menuList.isEmpty()) {
                List<AppChildMenu> appChildMenuList =
                        appChildMenuMapper.findList(new AppChildMenu(UserUtils.getUser().getId()));
                for (Menu m:menuList){
                    m.setChildren(getAllChildMenuBar(m, Lists.newArrayList(), appChildMenuList));
                }
            }
            return menuList;
        }
        return null;
    }


    public List<Menu> allChildMenus(Menu menu) {
        List<Menu> allMenuList = MenuUtils.getAllMenuList(menu);
        List<Menu> allChildMenuList = Lists.newArrayList();
        AppChildMenu appChildMenu = new AppChildMenu(UserUtils.getUser().getId());
        appChildMenu.setTenant(menu.getTenant());
        List<AppChildMenu> appChildMenuList = appChildMenuMapper.findList(appChildMenu);
        for (Menu m:allMenuList){
            getAllChildMenuBar(m, allChildMenuList, appChildMenuList);
        }
        return allChildMenuList;
    }

    public List<Menu> allChildMenuListByByParentIdsLike(Menu menu) {
        List<Menu> parentIdsLikeMenuList = MenuUtils.getByParentIdsLikeMenuList(menu);
        List<Menu> allChildMenuList = Lists.newArrayList();
        AppChildMenu appChildMenu = new AppChildMenu(UserUtils.getUser().getId());
        appChildMenu.setTenant(menu.getTenant());
        List<AppChildMenu> appChildMenuList = appChildMenuMapper.findList(appChildMenu);
        for (Menu childMenu:parentIdsLikeMenuList){
            getAllChildMenuBar(childMenu, allChildMenuList, appChildMenuList);
        }
        return allChildMenuList;
    }

    private List<Menu> getAllChildMenuBar(Menu menuItem, List<Menu> childMenuList, List<AppChildMenu> appChildMenuList) {
        List<Menu> children = menuItem.getChildren();
        if (children != null && !children.isEmpty()) {
            for (Menu menu:children) {
                if ("1".equals(menu.getType())) {
                    if (appChildMenuList != null && !appChildMenuList.isEmpty()) {
                        for (AppChildMenu appChildMenu:appChildMenuList) {
                            if (menu.getId().equals(appChildMenu.getMenuId())) {
                                menu.setIsDownload(1);
                                break;
                            }
                        }
                    }
                    childMenuList.add(menu);
                }
                getAllChildMenuBar(menu, childMenuList, appChildMenuList);
            }
        }
        return childMenuList;
    }
}
